import { queryBucket, addBucket, editBucket, delBucket, changeStatus } from '@/services/bucket';

export default {
  namespace: 'bucket',

  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetchByParams({ payload }, { call, put }) {
      const resp = yield call(queryBucket, payload);
      yield put({
        type: 'savePage',
        payload: resp.data,
      });
    },
    *add({ payload, callback }, { call }) {
      const response = yield call(addBucket, payload);
      if (response && response.state === 0) {
        if (callback) callback();
      }
    },
    *update({ payload, callback }, { call }) {
      const response = yield call(editBucket, payload);
      if (response && response.state === 0) {
        if (callback) callback();
      }
    },
    *remove({ payload, callback }, { call }) {
      const response = yield call(delBucket, payload);
      if (response && response.state === 0) {
        if (callback) callback();
      }
    },
    *changeStatus({ payload, callback }, { call }) {
      const response = yield call(changeStatus, payload);
      if (response && response.state === 0) {
        if (callback) callback();
      }
    },
  },

  reducers: {
    savePage(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveAll(state, action) {
      return {
        ...state,
        allAppInfos: action.payload,
      };
    },
    saveAllAnchorTypes(state, action) {
      return {
        ...state,
        allAnchorTypes: action.payload,
      };
    },
    saveAllEventTypes(state, action) {
      return {
        ...state,
        allEventTypes: action.payload,
      };
    },
    saveTree(state, action) {
      return {
        ...state,
        trees: action.payload,
      };
    },
  },
};
